package seqprint;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Semaphore;

/**
 * @author DELL shiny
 * @create 2020/1/3
 */
public class SeqPrintABC {

    private static Semaphore semaphoreA=new Semaphore(1);

    private static Semaphore semaphoreB=new Semaphore(0);

    private static Semaphore semaphoreC=new Semaphore(0);

    public static void printA(){
        System.out.print("A");
    }

    public static void printB(){
        System.out.print("B");
    }

    public static void printC(){
        System.out.print("C");
    }

    public static void main(String[] args) {
        new Thread(() -> {
            for(int i=0;i<100;i++){
                try {
                    semaphoreA.acquire();
                    printA();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                semaphoreB.release();
            }
        }).start();
        new Thread(() -> {
            for(int i=0;i<100;i++){
                try {
                    semaphoreB.acquire();
                    printB();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                semaphoreC.release();
            }
        }).start();
        new Thread(() -> {
            for(int i=0;i<100;i++){
                try {
                    semaphoreC.acquire();
                    printC();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                semaphoreA.release();
            }
        }).start();
    }
}
